//
//  LXSearchBarView.m
//  cat
//
//  Created by imera on 16/1/20.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXSearchBarView.h"

@interface LXSearchBarView()

@property (nonatomic, weak) UIButton *backBtn;

@property (nonatomic, weak) UIButton *searchBtn;


@end

@implementation LXSearchBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:backBtn];
    self.backBtn = backBtn;
    
    UITextField *textField = [[UITextField alloc]init];
    [self addSubview:textField];
    self.textField = textField;
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:searchBtn];
    self.searchBtn = searchBtn;
}

- (void)layoutSubviews
{
    UIButton *backBtn = self.backBtn;
    backBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    CGFloat backBtnW = 30;
    CGFloat backBtnH = 30;
    CGFloat backBtnX = LXSearchTableViewControllerMargin;
    CGFloat backBtnY = (self.frame.size.height - backBtnH) / 2;
    backBtn.frame = CGRectMake(backBtnX, backBtnY, backBtnW, backBtnH);
    
    UITextField *textField = self.textField;
    textField.tintColor = [UIColor grayColor];
    textField.placeholder = @"请输入关键词";
    textField.font = [UIFont systemFontOfSize:15];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    CGFloat textFieldW = self.frame.size.width * 0.75;
    CGFloat textFieldH = 30;
    CGFloat textFieldX = (self.frame.size.width - textFieldW) / 2;
    CGFloat textFieldY = (self.frame.size.height - textFieldH) / 2;
    textField.frame = CGRectMake(textFieldX, textFieldY, textFieldW, textFieldH);
    
    UIButton *searchBtn = self.searchBtn;
    [searchBtn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    CGFloat searchBtnW = 30;
    CGFloat searchBtnH = searchBtnW;
    CGFloat searchBtnX = self.frame.size.width - searchBtnW - LXSearchTableViewControllerMargin;
    CGFloat searchBtnY = (self.frame.size.height - searchBtnH) / 2;
    searchBtn.frame = CGRectMake(searchBtnX, searchBtnY, searchBtnW, searchBtnH);

}


- (void)back
{
    if ([self.MyDelagate respondsToSelector:@selector(popSelfController:)]) {
        [self.MyDelagate popSelfController:self];
    }
}

- (void)search
{
    if ([self.MyDelagate respondsToSelector:@selector(searchBarView:searchWithField:)]) {
        [self.MyDelagate searchBarView:self searchWithField:self.textField.text];
    }
}
@end
